package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcFriendApplys;
import com.cloud.bc.model.entity.BcFriends;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.ChatRequest;
import com.cloud.bc.model.request.LikeRequest;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.BcFriendApplysService;
import com.cloud.bc.service.BcFriendsService;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUsersService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/friend")
public class Ajax_FriendController {
    private final BcUsersService bcUsersService;
    private final BcFriendApplysService bcFriendApplysService;
    private final BcFriendsService bcFriendsService;
    private final BcSinglesService bcSinglesService;

    //我的好友列表
    @GetMapping("")
    public AjaxResp index(HttpServletRequest request, HttpServletResponse response,
                          LikeRequest req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        Page page = new Page();
        page.setCurrent(req.getPage());
        page.setSize(5);
        List<BcSingles> list = bcFriendsService.selectFriendList(page, userInfo.getId());
        ajaxResp.setList(list);
        return ajaxResp;
    }

    //新的好友
    @GetMapping("newfriend")
    public AjaxResp newfriend(HttpServletRequest request, HttpServletResponse response,
                              LikeRequest req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        Page page = new Page();
        page.setCurrent(req.getPage());
        page.setSize(5);
        List<BcSingles> list = bcFriendApplysService.selectNewFriendList(page, userInfo.getId());
        if (null != list) {
            list.forEach(item -> {
                item.setAge(bcSinglesService.ageConvert(item.getBirthday()));
            });
        }
        ajaxResp.setList(list);
        return ajaxResp;
    }

    //申请好友
    @GetMapping("apply/{id}")
    public AjaxResp apply(HttpServletRequest request, HttpServletResponse response,
                          @PathVariable("id") Integer id) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        //查询是否本来就是好友
        //查询是否正在申请中
        BcFriendApplys bcFriendApplys = new BcFriendApplys();
        bcFriendApplys.setUserId(userInfo.getId());
        bcFriendApplys.setTargetId(id);
        BcFriendApplys bcFriendApplysResult =
                bcFriendApplysService.selectFriendStatus(bcFriendApplys);
        if (null == bcFriendApplysResult) {
            //不存在申请记录
            //开始写入申请记录表
            bcFriendApplysService.save(bcFriendApplys);
            return ajaxResp;
        }
        ajaxResp.setSuccess(false);
        if (bcFriendApplysResult.getAgree().equals(3)) {
            //申请中
            ajaxResp.setMsg("申请好友中，请耐心等待对方");
        } else if (bcFriendApplysResult.getAgree().equals(1)) {
            ajaxResp.setMsg("已是好友，无需添加");
        } else if (bcFriendApplysResult.getAgree().equals(2)) {
            ajaxResp.setMsg("对方拒绝加为好友");
        }
        return ajaxResp;
    }

    //同意新好友
    @GetMapping("agree/{id}")
    public AjaxResp agree(HttpServletRequest request, HttpServletResponse response,
                          @PathVariable("id") Integer id) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        BcFriendApplys bcFriendApplys = new BcFriendApplys();
        bcFriendApplys.setAgree(1);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("target_id", userInfo.getId());
        queryWrapper.eq("user_id", id);
        bcFriendApplysService.update(bcFriendApplys, queryWrapper);
        BcFriends bcFriends = new BcFriends();
        bcFriends.setUserId(id);
        bcFriends.setTargetId(userInfo.getId());
        bcFriendsService.save(bcFriends);
        return ajaxResp;
    }

    //拒绝新好友
    @GetMapping("refuse/{id}")
    public AjaxResp refuse(HttpServletRequest request, HttpServletResponse response,
                           @PathVariable("id") Integer id) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        BcFriendApplys bcFriendApplys = new BcFriendApplys();
        bcFriendApplys.setAgree(2);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("target_id", userInfo.getId());
        queryWrapper.eq("user_id", id);
        bcFriendApplysService.update(bcFriendApplys, queryWrapper);
        return ajaxResp;
    }

}
